<?php
	class LoginControl extends Control{

		function __auto() {
        	$template = __ROOT__ . '/template/' . C('tpl_style');
        	$this->assign("template", $template);
    	}

		public function login(){
			$this->display();
		}

		public function logined(){
			$db=M('questions');
       		$result=$db->where('over="1"')->limit(12)->select();
        	$result1=$db->where('xs>60')->order('xs desc')->select();
        	$result2=$db->order('qid desc')->limit(4)->select();
        	$result3=$db->order('qid desc')->limit(1)->select();

        	$this->assign('result',$result);
        	$this->assign('result1',$result1);
        	$this->assign('result2',$result2);
        	$this->assign('result3',$result3);
        	$this->display();
		}

		public function runlogin(){

			if($this->isPOST()){
				$db=M("user");
				$db->validate=array(
					array("username","nonull","用户名不能为空",2),
					array("password","nonull","密码不能为空",2),
				);

				if(!$db->validate()){
					$this->error($db->error);
				}
				// $root=dirname(__ROOT__);
				$username=$_POST['username'];
				$user=$db->where('username="'.$username.'"')->find();
				if($db->where('username="'.$username.'"')->find()){
					if($user['password']==md5($_POST['password'])){
						$_SESSION['id']=$user['userid'];
						$_SESSION['name']=$user['username'];
						$this->success('登录成功','http://localhost/cms0121/member.php');
					}else{
						$this->error('密码错误');
					}
				}
			}else{
				_404("非法进入",__ROOT__);
			}
		}

		public function loginout (){
			session_unset();
	    	session_destroy();
	    	go("http://localhost/360/index.php");
		}

		public function regs(){
			$this->display();
		}
		
		public function runregs(){
			$db=M("user");
			$_POST['regtime']=time();
			// p($_POST);die;
			$data=array(
				'username'=>$_POST['username'],
				'password'=>md5($_POST['password']),
				'regtime'=>$_POST['regtime'],
				'email'=>$_POST['email'],
				'qq'=>$_POST['qq']
			);
			if($db->add($data)){
				$this->success('注册成功');
			}else{
				$this->error('注册失败');
			}
		}

		public function checkreg(){
			$db=M('user');
			$username=$_POST['username'];
			$result=$db->where('username="'.$username.'"')->find();
			if($result){
				echo json_encode(array('status' => 0, 'msg' => '<span style="color:red; margin-left:0">用户名已存在</span>'));
			}else{
				echo json_encode(array('status' => 1, 'msg' => '用户名可以使用'));
			}
		}

		// public function checkpwd(){
		// 	echo $_POST['pwd'];
		// 	echo $_POST['rpwd'];
		// 	if($_POST['pwd']==$_POST['rpwd']){
		// 		echo json_encode(array('status' => 1, 'msg' => ''));
		// 	}else{
		// 		echo json_encode(array('status' => 0, 'msg' => '两次密码不一致'));
		// 		return false;
		// 	}
		// }

		function code(){
			$code=new code();
			$code->show();
		}


		public function codecheck(){
			echo $_SESSION['verify'];
			if (strtolower($_POST['verify']) != strtolower($_SESSION['code'])) {
				echo json_encode(array('status'=>0,'msg'=>'<span style="color:red; margin-left:0">验证码错误 </span>'));
			}else{
				echo json_encode(array('status'=>1,'msg'=>'输入正确'));
			}
		}
	}
?>